package com.lansi.managers.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lansi.bean.ResponseResult;
import com.lansi.managers.bean.Manager;
import com.lansi.managers.service.IManagersService;

@Controller
@RequestMapping("/lansi")
public class ManagerController {
	//依赖注入
	@Resource
	private IManagersService managersService;
	
	//处理车辆人员信息注册
	@RequestMapping("/registerManager.do")
	@ResponseBody
	public ResponseResult<Void> registerManager(
			 /*String username,
			 String userkey,
			 String tel,
			 String usermail,
			 String prio*/
			 @RequestBody Map<String,String> map
			){
		ResponseResult<Void> rr = new ResponseResult<Void>();	
		try {
			
			String username =map.get("username");
			String userkey=map.get("userkey");
			String tel=map.get("tel");
			String usermail=map.get("usermail");
			//String prio=map.get("prio");
			
			System.out.println("manager用户注册 "+"username:"+username+"userkey:"+userkey+"tel:"+tel+"usermail:"+usermail);
			Manager manager = new Manager();
			manager.setUsername(username.trim());
			manager.setUserkey(userkey);
			manager.setTel(tel.trim());
			manager.setUsermail(usermail);
			manager.setCtime(new Date());
			manager.setPrio("1");
			
			managersService.register(manager);
			rr.setState(1);
			rr.setMessage("注册成功");			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());		
		}
				
		
		return rr;
		
	}

	//处理批量删除人员信息和单个删除人员信息
	@RequestMapping("/delManager.do")
	@ResponseBody
	public ResponseResult<Void> delManager(/*Integer[] ids*/  @RequestBody Map<String,Integer[]> map) {
		ResponseResult<Void> rr = new ResponseResult<Void>();
		try {
		  
		    Integer[]  ids = map.get("ids");
		   
			System.out.println("删除人员个数:"+ids.length);
			if(ids.length>1) {
				managersService.delManager(ids);
				rr.setState(1);
				rr.setMessage("删除成功");
			}else {
				Integer id = ids[0];
				managersService.delManagerById(id);
				rr.setState(1);
				rr.setMessage("删除成功");
			}
			
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}

		return rr;
	}
	
	//修改用户信息
	@RequestMapping("/updateManager.do")
	@ResponseBody
	public ResponseResult<Void> updateManager(
			/* Integer id,
			 String username,
			 String userkey,
			 String tel,
			 String usermail,
			 Date ctime,
			 String prio*/
			  @RequestBody Map<String,Object> map
			){
		ResponseResult<Void> rr = new  ResponseResult<Void>();
		try {
				Integer id 	=(Integer) map.get("id");
				String username =(String) map.get("username");
			//	String userkey=(String) map.get("userkey");
				String tel=(String) map.get("tel");
				String usermail=(String) map.get("usermail");
				String prio=(String) map.get("prio");
			 
			managersService.updateManager(id, username, null, tel, usermail, null, prio);
			rr.setState(1);
			rr.setMessage("修改成功");
		} catch (RuntimeException e) {
			rr.setState(-1);
			rr.setMessage(e.getMessage());
		}
		return rr;		
	}
	
	//查询出所有的用户信息
	@RequestMapping("/getManager.do")
	@ResponseBody
	public ResponseResult<List<Manager>> getManager(/*String username,String tel,Integer page*/ @RequestBody Map<String,Object> map){
		ResponseResult<List<Manager>> rr = new ResponseResult<List<Manager>>();
		try {			
			 Integer page	=(Integer) map.get("page");
			 String username =(String) map.get("username");
			 String tel=(String) map.get("tel");
			
			if(page==null) {
				page=1;
			}
			Integer offset = (page-1)*10;//跳过的页数
			List<Manager> list = managersService.getManager(username, tel, offset, 10);
			System.out.println("查询manager用户结果:"+list);
			Integer Managercount = managersService.getManagercount(username, tel);
			System.out.println("查询到的数量:"+Managercount);
			rr.setData(list);
			rr.setState(1);
			rr.setMessage(Managercount.toString());
		} catch (RuntimeException e) {
			rr.setMessage(e.getMessage());
			rr.setState(-1);			
		}
		return rr;
		
	}
}
